//
//  OANameListController.m
//  OASystem
//
//  Created by zhoupengfei on 15/4/21.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OANameListController.h"
#import "HttpTools.h"

#import "GDataDefines.h"
#import "MBProgressHUD.h"
#import "OANameListModel.h"
#import "OASubNameListModel.h"
#import "HeaderView.h"
#import "OANewEmailController.h"
#import "OANewSMSViewController.h"
#import "MKTreeView.h"

@interface OANameListController ()<HeaderViewDelegate,HeaderViewDelegate2>

@property (nonatomic,strong)MKTreeView *mkView;


@end

@implementation OANameListController
-(NSMutableArray*)groupArray{

    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

-(NSMutableArray*)rowArray{
    
    if (_rowArray == nil) {
        _rowArray = [NSMutableArray array];
    }
    return _rowArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self loadData];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(xxx:) name:@"11111" object:nil];

}

- (void)xxx:(NSNotification *)noti{
 NSMutableArray *array = noti.userInfo[@"text"];
    
     NSString *str = nil;

  for (NSString *str1 in array) {
    if (str.length == 0) {
   str  = [NSString stringWithFormat:@"%@",str1];

}else {
   str = [NSString stringWithFormat:@"%@,%@",str1,str];

}
    
    
    
  }
  _selectPersons = str;




}

#pragma mark 取消
-(IBAction)cancle:(id)sender{
//    [self.navigationController popViewControllerAnimated:YES];
  [self.navigationController popViewControllerAnimated:YES];


}
#pragma mark 确定
-(IBAction)confir:(id)sender{
if (_selectPersons) {
    

    
if ([_string isEqualToString:@"fasong"]){
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"segment":self.setpString},@{@"staff":_selectPersons},@{@"ryorder":@"1"},@{@"famxml":self.body}];
    
    NSLog(@"%@",array);

    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetStaffListDeliVerMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
    
            [self.view makeToast:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
 [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];

        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
    }];
}if ([_string isEqualToString:@"daiban"]){
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"segment":self.setpString},@{@"staff":_selectPersons},@{@"ryorder":@"1"},@{@"famxml":self.body}];
    
    NSLog(@"%@",array);

    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetStaffListDeliVerMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
    
            [self.view makeToast:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
// [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];

        int index=[[self.navigationController viewControllers]indexOfObject:self];

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index-2]animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
    }];
}
else{
NSMutableArray * tempArray = [NSMutableArray array];
    for (OANameListModel * nameLisModel in self.groupArray) {
        for (OASubNameListModel * subModel in nameLisModel.subArray) {
            if (subModel.isSelected) {
                [tempArray addObject:subModel];
                if ([self.delegate respondsToSelector:@selector(didSelecteName:)]) {
                    [self.delegate didSelecteName:tempArray];
                }
            }
        }
            }
  [self.navigationController popViewControllerAnimated:YES];
}
}
else{

[self.view makeToast:@"对不起，请选择人员"];
}
}

#pragma mark 加载网络数据
-(void)loadData{


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"json" ofType:nil];
    NSString *jsonString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
self.dataSource = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray *  array = @[@{@"userId":userId}];;

[HttpTools requestWithURL:@"exchTree" arrays:array method:@"globalTree" success:^(id JSON) {
NSLog(@"%@",JSON);

        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
NSArray *dataArray = dict[@"Parent"];
MKTreeView *view = [[MKTreeView instanceView] initTreeWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) dataArray:dataArray haveHiddenSelectBtn:NO haveHeadView:NO isEqualX:NO];
//    view.delegate = self;
    self.mkView = view;
    [self.view addSubview:view];

    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];



// MKTreeView *view = [[MKTreeView instanceView] initTreeWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) dataArray:dataArray haveHiddenSelectBtn:NO haveHeadView:NO isEqualX:NO];
////    view.delegate = self;
//    self.mkView = view;

//    [self.view addSubview:view];

}


-(void)clickHeaderView{
    [self.tableView reloadData];
}-(void)clickHeaderView2{
    [self.tableView reloadData];
}



//    OANameListModel * nameListModel = self.groupArray[indexPath.section];
//    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
//    subModel.isSelected = !subModel.isSelected;
//    [self.tableView reloadData];


@end
